package com.e3.sysmg.security.client;

import java.util.List;

import com.e3.sysmg.security.model.ModuleModel;
import com.e3.sysmg.security.model.RoleModel;
import com.e3.web.core.client.GXTHelper;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.core.client.QueryListener;
import com.e3.web.core.model.SuperQC;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.MultiField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.google.gwt.user.client.Element;

public class UserRoleGridField extends MultiField<RoleModel> implements QueryListener<RoleModel>{
	
	private RoleRPCServiceAsync service = RoleRPCService.Util.getInstance();
	private UserMessages MESSAGES = UserMessages.INSTANCE;
	
	protected AdapterField gridAdapter;
	protected AdapterField buttonAdapter;
	protected VerticalPanel buttonBar;

	private Grid<RoleModel> roleGridPanel;
	private GridSelectionModel<RoleModel> gridSelectionModel = new GridSelectionModel<RoleModel>();
	private ListStore<RoleModel> roleGridStore = new ListStore<RoleModel>();

	private Button addBtn = new MyButton(UserStateMachine.ADD_ROLE_ACTION);
	private Button removeBtn = new MyButton(UserStateMachine.REMOVE_ROLE_ACTION);
	private Button showModulesBtn = new MyButton(UserStateMachine.SHOW_ROLEMODULES_ACTION);
	private Button showAllModulesBtn = new MyButton(UserStateMachine.SHOW_ROLEALLMODULES_ACTION);
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private UserRoleQueryDialog roleQueryDialog;
	
	private ModuleDetailGridDialog moduleDetailDialog;
	
	public UserRoleGridField() {
		
		setSize(200, 225);

		gridAdapter = new AdapterField(getUserRoleGridPanel());
		
		buttonBar = new VerticalPanel();
		buttonBar.setSpacing(5);
		buttonBar.setStyleAttribute("margin", "3px");
		buttonBar.setHorizontalAlign(HorizontalAlignment.CENTER);
		buttonAdapter = new AdapterField(buttonBar);

		add(gridAdapter);
		add(buttonAdapter);
	}
	
	private Grid<RoleModel> getUserRoleGridPanel() {
		if(roleGridPanel == null){
			List<ColumnConfig> configs = RoleHelper.INSTANCE.getColumnConfigs();
			ColumnModel cm = new ColumnModel(configs);
			roleGridPanel = new Grid<RoleModel>(roleGridStore,cm);
			roleGridPanel.setSelectionModel(gridSelectionModel);
			
			roleGridStore.setModelComparer(RoleHelper.INSTANCE.getModelComparer());
			roleGridStore.setKeyProvider(RoleHelper.INSTANCE.getModelKeyProvider());
		}
		return roleGridPanel;
	}
	
	public ModuleDetailGridDialog getModuleDetailDialog(){
		if(moduleDetailDialog==null){
			moduleDetailDialog = new ModuleDetailGridDialog();
		}
		return moduleDetailDialog;
	}

	protected void initButtons() {
		
		int width = 100;
		addBtn.setWidth(width);
		removeBtn.setWidth(width);
		showModulesBtn.setWidth(width);
		showAllModulesBtn.setWidth(width);
		
		addBtn.addSelectionListener(btnSelectListener);
		removeBtn.addSelectionListener(btnSelectListener);
		showModulesBtn.addSelectionListener(btnSelectListener);
		showAllModulesBtn.addSelectionListener(btnSelectListener);
		
		buttonBar.add(addBtn);
		buttonBar.add(removeBtn);
		buttonBar.add(showModulesBtn);
		buttonBar.add(showAllModulesBtn);
	}
	
	@Override
	protected void onRender(Element target, int index) {
		initButtons();
		
		super.onRender(target, index);

	}
	
	@Override
	protected void onResize(int width, int height) {
		super.onResize(width, height);
		if (orientation == Orientation.HORIZONTAL) {
			int w = (width - buttonAdapter.el().getParent().getWidth());
			w -= (fields.size() * spacing);
			getUserRoleGridPanel().setSize(w, height);
		} else {
			for (Field<?> f : fields) {
				f.setWidth(width);
			}
		}
	}
	
	private UserRoleQueryDialog getUserRoleQueryDialog(){
		if(roleQueryDialog == null){
			roleQueryDialog = new UserRoleQueryDialog();
			roleQueryDialog.setHeading(addBtn.getText());
			roleQueryDialog.addQueryListener(this);
		}
		return roleQueryDialog;
	}

	private void onBtnMnuEvent(BaseAction action) {
		if(action == UserStateMachine.ADD_ROLE_ACTION){
			onAddRoleAction();
		}
		else if(action == UserStateMachine.REMOVE_ROLE_ACTION){
			onRemoveRoleAction();
		}
		else if(action == UserStateMachine.SHOW_ROLEMODULES_ACTION){
			onShowRoleModulesAction();
		}
		else if(action == UserStateMachine.SHOW_ROLEALLMODULES_ACTION){
			onShowRoleAllModulesAction();
		}
	}

	private void onShowRoleAllModulesAction() {
		List<RoleModel> modules = gridSelectionModel.getSelectedItems();
		if(modules == null||modules.size()==0){
			MessageBox.alert(MESSAGES.msgbox_alert_title(), MESSAGES.msg_alert_showdetail_module(), null);
			return;
		}
		
		GXTHelper.maskQuery(this);
		service.getAllModulesOfRole(modules.get(0), new GwtAsyncCallback<List<ModuleModel>>(this) {
			@Override
			public void doSuccess(List<ModuleModel> result) {
				getModuleDetailDialog().showDialog(result);
			}
		});
	}

	private void onShowRoleModulesAction() {
		List<RoleModel> models = gridSelectionModel.getSelectedItems();
		if(models == null||models.size()==0){
			MessageBox.alert(MESSAGES.msgbox_alert_title(), MESSAGES.msg_alert_showdetail_module(), null);
			return;
		}
		final RoleModel model = models.get(0);
		List<ModuleModel> moduleModels = model.getAuthedModules();
		if(moduleModels!=null&&moduleModels.size()>0){
			getModuleDetailDialog().showDialog(moduleModels);
			return;
		}
		GXTHelper.maskQuery(this);
		service.getRoleSelfModules(model, new GwtAsyncCallback<List<ModuleModel>>(this) {
			@Override
			public void doSuccess(List<ModuleModel> result) {
				unmask();
				model.setAuthedModules(result);
				getModuleDetailDialog().showDialog(result);
			}
		});
		
	}

	private void onAddRoleAction() {
		getUserRoleQueryDialog().setVisible(true);
	}

	private void onRemoveRoleAction(){
		List<RoleModel> models = getUserRoleGridPanel().getSelectionModel().getSelectedItems();
		if(models == null || models.isEmpty()){
			MessageBox.alert(MESSAGES.msgbox_alert_title(), MESSAGES.msg_alert_remove_role(), null);
			return;
		}
		for (RoleModel rm : models) {
			roleGridStore.remove(rm);
		}
	}
	
	public List<RoleModel> getGridValues(){
		return roleGridStore.getModels();
	}

	public void setGridValues(List<RoleModel> models){
		roleGridStore.removeAll();
		addGridValues(models);
	}
	
	public void addGridValues(List<RoleModel> models){
		if(models!=null&&models.size()>0){
			roleGridStore.add(models);
		}
	}
	
	public void onQuerySuccessed(SuperQC qc,List<RoleModel> results) {
		addGridValues(results);
	}

	@Override
	public void clear() {
		super.clear();
		roleGridStore.removeAll();
	}
	
	
}
